package top.drinyeit.weilong.adapter;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;

import java.util.List;

import top.drinyeit.weilong.R;
import top.drinyeit.weilong.activity.EditAddressActivity;
import top.drinyeit.weilong.activity.MyAddressActivity;
import top.drinyeit.weilong.dialog.CenterDialog;
import top.drinyeit.weilong.model.AddressBean;

/**
 * Created by bigHead on 2017/11/14.
 */

public class AddressAdapter extends BaseAdapter {


    private LayoutInflater mInflater;
    private List<AddressBean> list;
    private MyAddressActivity activity;

    public AddressAdapter(MyAddressActivity activity, List<AddressBean> list) {
        this.list = list;
        mInflater = LayoutInflater.from(activity);
        this.activity = activity;
    }

    public void addData(List<AddressBean> list) {
        this.list.addAll(list);
    }

    public void setData(List<AddressBean> list) {
        this.list = list;
    }

    public List<AddressBean> getData() {
        return list;
    }

    public void clear() {
        this.list.clear();
    }

//    public void remove(int position){
//        list.remove(position);
//        notifyItemRemoved(position);
//    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }


    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {

        final ViewHold viewHold;
        if (view == null) {
            viewHold = new ViewHold();
            view = mInflater.inflate(R.layout.item_address, null);
            viewHold.userName = view.findViewById(R.id.userName);
            viewHold.phone_number = view.findViewById(R.id.phone_number);
            viewHold.address = view.findViewById(R.id.address);
            viewHold.default_btn = view.findViewById(R.id.default_btn);
            viewHold.default_text = view.findViewById(R.id.default_text);
            viewHold.edit = view.findViewById(R.id.edit);
            viewHold.delete = view.findViewById(R.id.delete);
            view.setTag(viewHold);
        } else {
            viewHold = (ViewHold) view.getTag();
        }
//        if (list.get(i).getDefault_flag() == 1){
//            map.put(i,true);
//        }else {
//            map.put(i,false);
//        }
        viewHold.userName.setText("收货人: " + list.get(i).getConsignee_name());
        viewHold.phone_number.setText(list.get(i).getConsignee_phone());
        viewHold.address.setText("收货地址: " + list.get(i).getAddress_detail_json().getProvince() + list.get(i).getAddress_detail_json().getCity() +
                list.get(i).getAddress_detail_json().getRegion() + list.get(i).getAddress_detail_json().getDetail());
        if (list.get(i).getDefault_flag() == 1) {
            viewHold.default_btn.setSelected(true);
            viewHold.default_text.setText("默认地址");
        } else {
            viewHold.default_btn.setSelected(false);
            viewHold.default_text.setText("设为默认地址");
        }
        viewHold.default_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!viewHold.default_btn.isSelected()) {
                    activity.myAddressPresenter.setDe(list.get(i).getId(), i);
                }
            }
        });
        viewHold.edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra("address_id", list.get(i).getId());
                intent.putExtra("consignee_name", list.get(i).getConsignee_name());
                intent.putExtra("consignee_phone", list.get(i).getConsignee_phone());
                intent.putExtra("address_detail", new Gson().toJson(list.get(i).getAddress_detail_json()));
                intent.setClass(activity, EditAddressActivity.class);
                activity.startActivityForResult(intent, 1);
            }
        });
        viewHold.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final CenterDialog centerDialog_delete = new CenterDialog(activity, R.layout.dialog_center);
                centerDialog_delete.show();
                TextView dialog_text = centerDialog_delete.findViewById(R.id.dialog_text);
                dialog_text.setText("确定要删除该地址吗");
                TextView dialog_sure = centerDialog_delete.findViewById(R.id.dialog_sure);
                dialog_sure.setTextColor(ContextCompat.getColor(activity,R.color.red));
                dialog_sure.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        activity.myAddressPresenter.deleteAdd(list.get(i).getId());
                        centerDialog_delete.dismiss();
                    }
                });
                TextView dialog_cancel = centerDialog_delete.findViewById(R.id.dialog_cancel);
                dialog_cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        centerDialog_delete.dismiss();
                    }
                });
            }
        });
//        if(map!=null&&map.containsKey(i)){
//            viewHold.default_btn.setSelected(true);
//        }else {
//            viewHold.default_btn.setSelected(false);
//        }

        return view;
    }

    public void isDe(int j) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getDefault_flag() == 1) {
                list.get(i).setDefault_flag(0);
                break;
            }
        }
        list.get(j).setDefault_flag(1);
    }


    private final class ViewHold {
        TextView userName;
        TextView phone_number;
        TextView address;
        ImageView default_btn;
        TextView default_text;
        TextView edit;
        TextView delete;
    }



}
